package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.PlanningTemplateDaoIn;
import com.aerotechnic.entities.Planning;
import com.aerotechnic.entities.PlanningTemplate;

@Repository("PlanningTemplateDao")
public class PlanningTemplateDao implements PlanningTemplateDaoIn {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(PlanningTemplate t) {

		// TODO Auto-generated method stub

		getCurrentSession().save(t);

	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub

		PlanningTemplate e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);

	}

	@Override
	public PlanningTemplate findByID(Long id) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.getCurrentSession();

		return (PlanningTemplate) session
				.createQuery("FROM PlanningTemplate where id= " + id).list()
				.get(0);
	}

	@Override
	public void update(PlanningTemplate t) {
		// TODO Auto-generated method stub

		getCurrentSession().update(t);
	}

	public List<PlanningTemplate> findAll() {
		// TODO Auto-generated method stub

		return getCurrentSession().createQuery("from PlanningTemplate").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PlanningTemplate> getPlTemplWithLessCharge(String typeCheck,
			String typeAvion, float charge2notExcede) {
		// TODO Auto-generated method stub

		return getCurrentSession().createQuery(
				"from PlanningTemplate  where typeCheck = '" + typeCheck
						+ "' and typeAvion = '" + typeAvion + "' and charge <= "
						+ charge2notExcede).list();   
	}
}
